//
//  UIButton+FCUrlPath.m
//  Pods
//
//  Created by WJC on 2020/12/17.
//

#import "UIButton+FCUrlPath.h"
#import "NSObject+FCUrlPathGet.h"
#import "NSObject+KVCChangeType.h"
@implementation UIButton (FCUrlPath)
static const char *key = "actionKey";
static const char *successKey = "successKey";
-(void)setPushController:(NSString *)pushController{
    objc_setAssociatedObject(self, @selector(pushController), pushController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    WJCWeakSelf
    [self addTargetWithBlock:^(UIButton * _Nonnull button) {
        
        if ([pushController.lowercaseString containsString:@"viewcontroller"]) {
            [[FCCGetCurrentVcTool getCurrentViewController] pushAppControllerWithControllerName:pushController andObjectData:weakSelf.requestData andTitle:self.txtTag?self.txtTag:nil];
        }
        
    }];
}
-(NSString *)pushController{
    return objc_getAssociatedObject(self, @selector(pushController));
}
-(void)setFilterError:(bool)filterError{
    objc_setAssociatedObject(self, @selector(filterError), @(filterError), OBJC_ASSOCIATION_ASSIGN);
}
-(bool)filterError{
    return objc_getAssociatedObject(self, @selector(filterError));
}
-(void)setStopRequest:(bool)stopRequest{
    objc_setAssociatedObject(self, @selector(stopRequest), @(stopRequest), OBJC_ASSOCIATION_ASSIGN);
}
-(bool)stopRequest{
    return objc_getAssociatedObject(self, @selector(stopRequest));
}
-(void)setUrlPath:(NSString *)urlPath{
    if ([urlPath isKindOfClass:[NSString class]] && [[urlPath substringToIndex:1] isEqual:@"/"]) {
        urlPath = [urlPath substringFromIndex:1];
    }
    objc_setAssociatedObject(self, @selector(urlPath), urlPath, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

-(NSString *)requestStrMJClass{
    return objc_getAssociatedObject(self, @selector(requestStrMJClass));
}
-(void)setRequestStrMJClass:(NSString *)requestStrMJClass{
    objc_setAssociatedObject(self, @selector(requestStrMJClass), requestStrMJClass, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
}
-(NSObject *)mjClass{
    return objc_getAssociatedObject(self, @selector(mjClass));
}
-(void)setMjClass:(NSObject *)mjClass{
    objc_setAssociatedObject(self, @selector(mjClass), mjClass, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)urlPath{
    return objc_getAssociatedObject(self, @selector(urlPath));
}
-(void)setRequestData:(NSDictionary *)requestData{
    objc_setAssociatedObject(self, @selector(requestData), requestData, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(NSDictionary *)requestData{
    return objc_getAssociatedObject(self, @selector(requestData));
}
-(void)addTargetWithState:(UIControlEvents)state Block:(ButtonActionBlock)actionBlock{
    [self addTarget:self action:@selector(buttonClick:) forControlEvents:state];
        objc_setAssociatedObject(self, key, actionBlock, OBJC_ASSOCIATION_COPY);
}
-(void)addTargetWithBlock:(ButtonActionBlock)actionBlock{
    [self addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        objc_setAssociatedObject(self, key, actionBlock, OBJC_ASSOCIATION_COPY);
}
-(void)buttonClick:(UIButton *)button{
   ButtonActionBlock actionBlock1 = objc_getAssociatedObject(button, key);
   if (actionBlock1) {
       actionBlock1(button);
    }
}
-(void)addTargetRequestWithBlock:(requestSuccetionActionBlock)success{
    [self addTarget:self action:@selector(requestClick:) forControlEvents:UIControlEventTouchUpInside];
        objc_setAssociatedObject(self, successKey, success, OBJC_ASSOCIATION_COPY);
}
-(void)requestClick:(UIButton *)button{
    if(self.stopRequest) return;
    requestSuccetionActionBlock successBlock = objc_getAssociatedObject(button, successKey);
    button.enabled = NO;
    NSString *urlStr;
    if ([self.urlPath isUrl]) {
        urlStr = self.urlPath;
    }else{
        if (self.urlPath) {
            urlStr = [NSString stringWithFormat:@"%@%@",[WJCDeployModel getDeployWithDeployKey:WJCDeployAPIHeadUrl],self.urlPath];
        }
    }
    [self postRequestWithBlock:successBlock];
//    [FCBaseTool postWithUrl:urlStr param:self.requestData resultClass:nil keyValues:YES success:^(id obj) {
//        button.enabled = YES;
//        if (successBlock) {
//            [[NSNotificationCenter defaultCenter] postNotificationName:self.urlPath object:nil];
//            if(self.requestStrMJClass.length>0){
//                NSArray *arr = [self.requestStrMJClass componentsSeparatedByString:@","];
//                id object = [obj skipGraphQLItems];
//                for (NSString *str in arr) {
//                    if (!object) {
//                        successBlock(obj);
//                        WJCLog(@"NSObject+KVCChangeType中requestClick数据找不到'%@'",str);
//                        break;
//                    }
//                    if ([object isKindOfClass:[NSArray class]]) {
//                        object = object[[str integerValue]];
//                    }else{
//                        object = object[str];
//                    }
//                }
//                if ([object isKindOfClass:[NSArray class]]) {
//                    if (self.mjClass) {
//                        successBlock([[self.mjClass class] mj_objectArrayWithKeyValuesArray:object]);
//                    }else{
//                        successBlock(object);
//                    }
//                    
//                }else{
//                    if (self.mjClass) {
//                        successBlock([[self.mjClass class] mj_objectWithKeyValues:object]);
//                    }else{
//                        successBlock(object);
//                    }
//                }
//                
//            }else{
//                successBlock(obj);
//            }
//        }
//        
//    } failure:^(NSError *error) {
//        button.enabled = YES;
//    }];

}
@end
